package Servlet;

import Pojo.Journalism;
import Pojo.NewInfo;
import Service.ViceCheck;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Limit")
public class Limit extends HttpServlet {
    public void fan(int index, HttpServletRequest req){
        ViceCheck viceCheck = new ViceCheck();
        List<NewInfo> newInfo = viceCheck.findinfo(index);
        req.getSession().removeAttribute("newInfo");
        req.getSession().setAttribute("newInfo",newInfo);
        req.getSession().removeAttribute("index");
        req.getSession().setAttribute("index",index);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int  x = Integer.parseInt(req.getParameter("jump"));
        fan((x-1)*10+1,req);
        resp.getWriter().print(1);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String what = req.getParameter("what");
//        前一页
        int index = (int) req.getSession().getAttribute("index");
        if (what.equals("up")) {
            if(index-10<1){
//                当前是第一页
                resp.getWriter().print(1);
            }else{
                index-=10;
//            向前翻页后重新加载
                fan(index,req);
                resp.getWriter().print(2);
            }
        } else if(what.equals("down")){
//            后一页
            Journalism j = (Journalism) req.getSession().getAttribute("journalism");
            int x = j.getNewnum();
            if(index+10>x){
//                当前是最后一页
                resp.getWriter().print(1);
            }else{
                index+=10;
//            向后翻页后重新加载
                fan(index,req);
                resp.getWriter().print(2);
            }

        }

    }
}
